package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cinema.BO.YzOrderInfoReqBO;
import com.ruoyi.order.BO.GoodsOrderFinishMsgBO;
import com.ruoyi.order.DTO.OrderInfoYzDTO;
import com.ruoyi.cinema.service.impl.CustomerServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.common.constant.WechatConstants;
import com.ruoyi.common.core.redis.RedisCache;

import com.ruoyi.utils.YzResult;
import com.ruoyi.utils.YzUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component("WechatTask")
public class WechatTask {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private CustomerServiceImpl customerService;
    @Autowired
    private OrderMapper orderMapper;

    public void sendOrderFinishMessage(){
        Set<ZSetOperations.TypedTuple<String>> set =  redisCache.zRangeWithScores(WechatConstants.WEIXIN_MESSAGE_ORDER_FINISH_PRE,0,-1);
        if(set.isEmpty()){
            return;
        }
        Long timestamp = System.currentTimeMillis();
        for (ZSetOperations.TypedTuple<String> t :set) {
            if(null == t.getScore()){
                redisCache.removeZSet(WechatConstants.WEIXIN_MESSAGE_ORDER_FINISH_PRE,t.getValue());
                continue;
            }
            if(timestamp > t.getScore().longValue()){
                YzOrderInfoReqBO yzOrderInfoReqBO = JSONObject.parseObject(t.getValue(), YzOrderInfoReqBO.class);
                String accessToken = customerService.getAccessToken(yzOrderInfoReqBO.getCustomerId(),yzOrderInfoReqBO.getPhone());
                //String accessToken = "ac50f42244924fda835d8e2a9ff1ace3";
                if(null == accessToken){
                    return;
                }
                YzResult<OrderInfoYzDTO>  result = YzUtils.getOrderInfo(yzOrderInfoReqBO.getCinemaLinkId(),yzOrderInfoReqBO.getYzOrderId(),accessToken);
                if(result.getResultCode().equals(Constants.YZSUCCESS) && result.getData().getBizCode().equals(Constants.BIZSUCCESS)){
                    OrderInfoYzDTO orderInfoYzDTO = result.getData().getBizData();
                    if(null != orderInfoYzDTO && null != orderInfoYzDTO.getFulfillInfo() && null != orderInfoYzDTO.getFulfillInfo().getFulfillStatus()){
                       String fulStatus = orderInfoYzDTO.getFulfillInfo().getFulfillStatus();
                        if(fulStatus.equals("WAIT_SELF") ||fulStatus.equals("WAIT_DELIVER")){
                            GoodsOrderFinishMsgBO gofb = orderMapper.selectGOMsgBO(yzOrderInfoReqBO.getOrderId());
                            customerService.sendGoodsCanGetMessage(gofb);
                        }
                    }
                }
                redisCache.removeZSet(WechatConstants.WEIXIN_MESSAGE_ORDER_FINISH_PRE,t.getValue());
            }else {
                return;
            }
        }
    }
}
